package com.xiaoma.tpo.interceptor;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.poi.util.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

import com.mysql.jdbc.StringUtils;

public class CommonInterceptor extends HandlerInterceptorAdapter {
	 private final Logger log = LoggerFactory.getLogger(CommonInterceptor.class);  
	    public static final String LAST_PAGE = "com.alibaba.lastPage";  
	     
	    @Override    
	    public boolean preHandle(HttpServletRequest request,    
	            HttpServletResponse response, Object handler) throws Exception {   
	        log.info("==============执行顺序: 1、preHandle================");    
	        String requestUri = request.getRequestURI();  
	        String contextPath = request.getContextPath();  
	        String url = requestUri.substring(contextPath.length());  
	        
	        log.info("requestUri:"+requestUri);    
	        log.info("contextPath:"+contextPath);    
	        log.info("url:"+url);    
	        String username =  (String)request.getSession().getAttribute("username"); 
	        //先判断用户是否登录
	        if(username == null){  
	            log.info("Interceptor：跳转到login页面！");  
	            request.getRequestDispatcher("/WEB-INF/views/login.jsp").forward(request, response);  
	            return false;  
	        }else{
	        	//在判断用户有哪些权限
	        	
	        	return true;     
	        }  
	    }    
	   
	    @Override    
	    public void postHandle(HttpServletRequest request,    
	            HttpServletResponse response, Object handler,    
	            ModelAndView modelAndView) throws Exception {     
	        log.info("==============执行顺序: 2、postHandle================");    
	        if(modelAndView != null){  //加入当前时间    
	            modelAndView.addObject("var", "测试postHandle");    
	        }    
	    }    
	    
	    @Override    
	    public void afterCompletion(HttpServletRequest request,    
	            HttpServletResponse response, Object handler, Exception ex)    
	            throws Exception {    
	        log.info("==============执行顺序: 3、afterCompletion================");    
	    }    
}
